<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/1/7
 * Time: 23:42
 */
namespace app\model;

use think\facade\Db;
use think\Model;

class SuppliesApplyList extends Model
{
    protected $table = 'supplies_apply_list';
    private function _getListField()
    {
        return [
            'sa_id' => 'id',
            'sa_create_time' => 'createTime',
        ];
    }

    private function _getDetailListField()
    {
        return [
            'sal_id' => 'id',
            's_id' => 'suppliesId',
            's_name' => 'name',
            'sal_num' => 'num',
            's_unit' => 'unit',
        ];
    }

    public function getApplyListByApplyId($applyId)
    {
        $where = [
            'sal_apply_id' => $applyId
        ];

        return $this->leftJoin('supplies','s_id=sal_supplies_id')
            ->where($where)
            ->field($this->_getDetailListField())
            ->select() ?? [];
    }
    public function getSuppliesInfoById($id,$field = '')
    {
        $field = $field ?: self::_getListField();
        return Db::table('supplies')
            ->leftJoin('user','user.id=s_uid')
            ->leftJoin('supplies_type','s_type=st_id')
            ->leftJoin('organization','organization.id=s_orgid')
            ->where(['s_id'=>$id])->field($field)->find();
    }

    public function addSuppliesApplyList($list)
    {
        return $this->insertAll($list);
    }

    public function getApplyListByCondition($where = [],$order = 'sa_id desc',$field = '')
    {
        $field = $field ?: self::_getListField();
        $list = Db::table('supplies_apply')
            ->leftJoin('supplies','s_id=sa_supplies_id')
            ->where($where)
            ->field($field)
            ->order($order)
            ->paginate(input('param.pageSize'))
            ->render();
        return $list;
    }

}